/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */

/**
 * Constructor
 */
function BlogRssReader(){
	this.jsjava_class="jsorg.eob.blog.BlogRssReader";	
};

/**
 * Read the rss content
 * @param xmlStr the rss content
 */
BlogRssReader.prototype.readRss=function(xmlStr){	
	var parser=new XmlBrowserParser();
	parser.loadXml(xmlStr);
	var blogdom=parser.toDocument();
	if(!blogdom){
		return null;
	}
	var blogRss=new BlogRss();
	var rootElem=blogdom.documentElement;
	if(!rootElem){
		return null;
	}
	var version=rootElem.getAttribute("version");
	blogRss.setVersion(version);
	var channel=new BlogRssChannel();
	var channelElem=rootElem.selectSingleNode("channel");
	var ctitle=getNodeValue(channelElem,"title");		
	var clink=getNodeValue(channelElem,"link");
	var cdescription=getNodeValue(channelElem,"description");;
	var cgenerator=getNodeValue(channelElem,"generator");
	channel.setTitle(ctitle);
	channel.setLink(clink);
	channel.setDescription(cdescription);
	channel.setGenerator(cgenerator);
	var channelImage=new BlogRssChannelImage();
	var imageElem=channelElem.selectSingleNode("image");
	if(imageElem){
		var imageNodeType=imageElem.firstChild.nodeType;
		if(imageNodeType==3){
			channelImage.setURL(imageElem.firstChild.nodeValue);
		}else{
			var imageTitle=getNodeValue(imageElem,"title");
			var imageLink=getNodeValue(imageElem,"link");
			var imageURL=getNodeValue(imageElem,"url");		
			channelImage.setTitle(imageTitle);
			channelImage.setLink(imageLink);
			channelImage.setURL(imageURL);	
		}
		channel.setImage(channelImage);
	}
	itemElems=channelElem.selectNodes("item");
	var itemElemsLenght=itemElems.length;
	for(var i=0;i<itemElemsLenght;i++){
		var itemElem=itemElems[i];
		var item=new BlogRssItem();
		var icreator=getNodeValue(itemElem,"dc:creator");
		if(icreator==null){
			icreator=getNodeValue(itemElem,"author");
		}
		var ititle=getNodeValue(itemElem,"title");
		var ilink=getNodeValue(itemElem,"link");
		var iguid=getNodeValue(itemElem,"guid");
		var idescription=getNodeValue(itemElem,"description");
		var ipubDate=getNodeValue(itemElem,"pubDate");
		item.setCreator(icreator);
		item.setTitle(ititle);
		item.setLink(ilink);
		item.setId(iguid);
		item.setDescription(idescription); 
		item.setPubDate(ipubDate);
		var commentId=getNodeValue(itemElem,"wfw:comment");
		var commentForItemId=getNodeValue(itemElem,"comments");
		var commentRss=getNodeValue(itemElem,"wfw:commentRss");
		var comment=new BlogRssItemComment();
		comment.setId(commentId);
		comment.setItemId(commentForItemId);
		comment.setCommentRss(commentRss);
		item.setComments(comment);
		channel.addItem(item);
	}
	blogRss.setRssChannel(channel);
	return blogRss;
	function getNodeValue(parentElem,elemTagName){
		var value=null;
		var valueElem=parentElem.selectSingleNode(elemTagName+"/text()");
		if(valueElem){
			value=valueElem.nodeValue;
		}
		return value;
	}
};